package com.etcn.common.schedule;

import java.util.Date;
import java.util.List;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.etcn.modules.busi.order.pojo.WmOrder;

import ch.qos.logback.classic.Logger;

/**
 * 订单状态定时检查
 * 
 * @author guozc
 */
@Component
public class OrderSchedule {
	private Logger serviceLogger = (Logger) LoggerFactory.getLogger("service_file_log");
	@Autowired
	@Qualifier("busiJdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Value("${order.expire.days}")
	private int orderExpireDays;

	@Scheduled(cron = "0 0/5 * * * ?")
	public void check() throws Exception {
		serviceLogger.info("开始检查过期订单......");
		String sql = "select order_sn,create_time from wm_order where order_status = '1'";
		List<WmOrder> orders = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WmOrder.class));
		if(orders != null){
			for(WmOrder o : orders){
				int days = (int)((new Date().getTime() - o.getCreateTime().getTime())/(1000 * 60 * 60 * 24));
				if(days >= orderExpireDays){
					sql = "update wm_order set order_status = ? where order_sn = ?";
					jdbcTemplate.update(sql, new Object[]{"3", o.getOrderSn()});
					serviceLogger.info("更新订单【"+o.getOrderSn()+"】状态为已失效");
				}
			}
		}
	}
}
